package cn.lili.controller.wallet;

import cn.lili.common.enums.ResultUtil;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.wallet.entity.dto.MemberWalletDTO;
import cn.lili.modules.wallet.entity.enums.WalletOwnerEnum;
import cn.lili.modules.wallet.entity.vo.MemberWalletSearchParams;
import cn.lili.modules.wallet.entity.vo.MemberWalletVO;
import cn.lili.modules.wallet.service.MemberWalletService;
import cn.lili.modules.whitebar.entity.vo.CommissionSearchParams;
import cn.lili.modules.whitebar.entity.vo.CommissionVO;
import cn.lili.modules.whitebar.service.CommissionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理端,预存款接口
 *
 * @author pikachu
 * @since 2020/11/16 10:07 下午
 */
@RestController
@Api(tags = "管理端,预存款接口")
@RequestMapping("/manager/members/wallet")
public class MemberWalletManagerController {
    @Autowired
    private MemberWalletService memberWalletService;
    @Autowired
    private CommissionService commissionService;


    @GetMapping
    @ApiOperation(value = "查询会员预存款余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", paramType = "query"),
            @ApiImplicitParam(name = "owner", value = "会员来源", paramType = "query")
    })
    public ResultMessage<MemberWalletVO> get(@RequestParam("memberId") String memberId,@RequestParam("owner") String owner) {
        return ResultUtil.data(memberWalletService.getMemberWallet(memberId,owner ));
    }


    @PostMapping(value = "/all")
    @ApiOperation(value = "平台钱包收入汇总")
    public ResultMessage<Map<String ,Object>> all() {
        Map<String ,Object> map = new HashMap<>();
        //支出
        CommissionSearchParams commissionSearchParams = new CommissionSearchParams();
        commissionSearchParams.setPageSize(999999999);
        IPage<CommissionVO> commissionVO = commissionService.findCommissionVO(commissionSearchParams);
        Double outMoney = 0D;
        if(CollectionUtils.isNotEmpty(commissionVO.getRecords())){
            for (CommissionVO c : commissionVO.getRecords()) {
                outMoney = CurrencyUtil.add(c.getFlowPrice() != null ? c.getFlowPrice() : 0D ,outMoney);
            }
        }
        map.put("outMoney",outMoney);
        //收入
        MemberWalletSearchParams memberWalletSearchParams = new MemberWalletSearchParams();
        memberWalletSearchParams.setPageSize(999999999);
        IPage<MemberWalletDTO> memberWalletDTO = memberWalletService.findMemberWalletDTO(memberWalletSearchParams);
        Double inMoney = 0D;
        if(CollectionUtils.isNotEmpty(memberWalletDTO.getRecords())){
            for (MemberWalletDTO me : memberWalletDTO.getRecords()) {
                inMoney = CurrencyUtil.add(me.getMoney() != null ? me.getMoney() : 0D,inMoney);
            }
        }
        map.put("inMoney",inMoney);
        map.put("allMoney",CurrencyUtil.sub(inMoney,outMoney));
        return ResultUtil.data(map);
    }


    @PostMapping(value = "/outlay")
    @ApiOperation(value = "平台钱包支出明细")
    public ResultMessage<IPage<CommissionVO>> outlay(CommissionSearchParams commissionSearchParams) {
        return ResultUtil.data(commissionService.findCommissionVO(commissionSearchParams));
    }

    @PostMapping(value = "/income")
    @ApiOperation(value = "平台钱包收入明细")
    public ResultMessage<IPage<MemberWalletDTO>> income(MemberWalletSearchParams memberWalletSearchParams) {
        return ResultUtil.data(memberWalletService.findMemberWalletDTO(memberWalletSearchParams));
    }

}
